<?php
class Admin_Article extends Admin_Controller{
	public $section = 'article';
	private $path_article = "./uploads/news/";
	//private $path_article_thumbs = "./uploads/news/thumbs/";
	private $path_upload = "";
//	private $path_thumbs = "";
//	private $path_home = "";
	private $folder;
	private $setting;
	private $page_size = 10;
	
	function __construct(){
		parent::__construct();
		$this->check_permissions();
		//cau hinh cho ckfinder tao va nhan module news
		//$this->session->set_userdata('module','ck_'.$this->module);
		//@session_start();
		$_SESSION['module'] = "ck_".$this->module;
//		echo $_SESSION['module'];exit;
		
		$this->load->model('article_m');
		$this->load->model('categories_m');
		
		//load config
		$this->config->load('admin');
		
		
		//so tin 1 trang cua admin
		$this->page_size = $this->config->item('page_size');
		
		//setting
		$this->load->model('settings/settings_m');
		//tao thu muc
		$this->folder = date("Y_W/");
		$this->path_upload = $this->path_article.$this->folder;
		//$this->path_thumbs = $this->path_article_thumbs.$this->folder;
		if(!is_dir( $this->path_upload)){
			mkdir($this->path_upload, 0777);
			//mkdir($this->path_thumbs, 0777);
			
		}
		$setting = $this->settings_m->get_all($this->module);
		$this->setting = array_for_select($setting,'name','value');
	}
	
	public function index( $cat_id = NULL , $page_index = 0 ){
		
		if($cat_id)
			$this->action = 'new|'.site_url('admin/news/article/create/'.$cat_id).'|'.lang('global:addnew').
		'+ remove|javascript:void(0)|'.lang('global:delete_selected');
		

		$base_where = array();
		$base_where = $this->input->post('f_page') ? $base_where + array('limit' => $this->input->post('f_page')) : $base_where;
		$base_where = $cat_id ? $base_where + array('cat_id' => $cat_id) : $base_where;

		$model = $this->article_m->get_many_by($base_where,$this->page_size);//tra ve articles + total_row
	
		 
		
		$this->load->helper('kunkun_page');
		$total_page = total_page($model->total_row,$this->page_size);
		
		if ($this->input->is_ajax_request())
		{
			$this->template->set_layout(false);
		}
		
		$cat = $this->categories_m->get_all();
		$level1 = array();
		array_level1($cat,$level1);
		$cat_select = array_for_select($level1,'id','title','frefix');
		
		$this->template
			->set('model',$model->articles)
			->set('cat_select',$cat_select)
			->set('cat_id',$cat_id)
			->set('total_page',$total_page)
			
			
			//->set('pagination',$pagination)
			
			->set('action',$this->action)
			->append_js('filter.js');
			//->set_partial('filters', 'salary/partials/filters');
		
		$this->input->is_ajax_request() ? $this->template->build($this->controller.'/partial/list') : $this->template->build($this->controller.'/'.$this->method);
			
	}
	
	public function create( $cat_id = NULL ){
				
		$model = $this->input->post();
		
		$this->form_validation->set_rules('title',lang('news:title'),'required|max_length[255]');
		$this->form_validation->set_rules('cat_id',lang('news:categories'),'required|max_length[80]');
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				//print_r($model);exit;
				$skip = array('btnAction');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); // bo những biến post wa ko phải dữ liệu
					}
				}
				//UPLOAD IMAGE - News chi up va resize ko can save hinh f
				$this->config->load('upload');
				
							
				$config = $this->config->item('image');
				$config['upload_path'] = $this->path_upload;
				$config['max_width'] = $this->setting['max_width'];
				$config['max_height'] = $this->setting['max_height'];
				$config['overwrite'] = TRUE; // thay the hinh lon = hinh resize
				$this->load->library('upload',$config);

				if($this->upload->do_upload('upload')){
					$image = $this->upload->data();
					
					//tao THUMB
					$this->load->library('image_lib');
					$thumb = $this->config->item('image_thumb');
					$thumb['source_image'] = $image['full_path'];
					$thumb['new_image'] = $this->path_upload;//$this->path_thumbs;
					$thumb['width'] = $this->setting['thumbs_width'];
					$thumb['height'] = $this->setting['thumbs_height'];
				
					$this->image_lib->initialize($thumb);
					$this->image_lib->resize();
					$model['image'] = $this->folder.$image['file_name'];
				//	
				}
				//END:UPLOAD IMAGE			
				$id_insert = $this->article_m->insert($model);
				if($type == 'save')
					redirect('admin/'.$this->module.'/'.$this->section);
				if($type == 'add')
					redirect('admin/'.$this->module.'/'.$this->section.'/'.$this->method);
				if($type == 'edit')
					redirect('admin/'.$this->module.'/'.$this->section.'/edit/'.$id_insert);
				
			}
		}
		$categories_radio = $this->categories_m->list_categories_radio(NULL,$name='cat_id', 0, NULL );
		$this->template
			->set('model',$model)
			->set('cat_id',$cat_id)
			->set('categories_radio',$categories_radio)
			->append_js('jquery.tabify.js')
			->build($this->controller.'/form');
	}
	
	public function edit( $id ){
		$model = (array)$this->article_m->get($id);

		
		$this->form_validation->set_rules('title',lang('news:title'),'required|max_length[80]');
		$this->form_validation->set_rules('cat_id',lang('news:categories'),'required|max_length[80]');
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model)){
					unset($model[$obj]); // bo những biến post wa ko phải dữ liệu
					}
				}
				//UPLOAD IMAGE - News chi up va resize ko can save hinh f
				$this->config->load('upload');
				
							
				$config = $this->config->item('image');
				$config['upload_path'] = $this->path_upload;
				$config['max_width'] = $this->setting['max_width'];
				$config['max_height'] = $this->setting['max_height'];
				$config['overwrite'] = TRUE; // thay the hinh lon = hinh resize
				$this->load->library('upload',$config);

				if($this->upload->do_upload('upload')){
					$image = $this->upload->data();
					
					//tao THUMB
					$this->load->library('image_lib');
					$thumb = $this->config->item('image_thumb');
					$thumb['source_image'] = $image['full_path'];
					$thumb['new_image'] = $this->path_upload;//$this->path_thumbs;
					$thumb['width'] = $this->setting['thumbs_width'];
					$thumb['height'] = $this->setting['thumbs_height'];
				
					$this->image_lib->initialize($thumb);
					$this->image_lib->resize();
					$model['image'] = $this->folder.$image['file_name'];
				//	
				}
				//END:UPLOAD IMAGE			
				$this->article_m->update($id,$model);
				if($type == 'save')
					redirect('admin/'.$this->module.'/'.$this->section);
				if($type == 'add')
					redirect('admin/'.$this->module.'/'.$this->section.'/'.$this->method);
				if($type == 'edit')
					redirect('admin/'.$this->module.'/'.$this->section.'/edit/'.$id);
				
			}
		}
		
		$categories_radio = $this->categories_m->list_categories_radio(NULL,$name='cat_id', 0, NULL );
		$this->template
			->set('model',$model)
			->set('categories_radio',$categories_radio)
			->set('cat_id',$model['cat_id'])
			->append_js('jquery.tabify.js')
			->build($this->controller.'/form');
	}
	
	public function delete($id){
		$this->article_m->delete($id);
		redirect('admin/news/article');
	}
	
	
}